// pages/agency/agency.js
const SERVICE = require("../../service.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    let openid = wx.getStorageSync("token");        
    wx.showNavigationBarLoading();
    SERVICE.getMyAgencyInfo({openid},(status,data)=>{
      console.log("会员数据:", data);
      wx.hideNavigationBarLoading();
      
      if (status){
       _this.setData({
         userInfo: data
       }); 
     }else{
        _this.setData({
          userInfo: data.data
        }); 
     }
    
    });

    SERVICE.getAgencSilder({openid},(status,data)=>{
      if(status){
        _this.setData({
          silderData:data.data
        });
      }
    });

    SERVICE.getQRcode({ type: 1, openid }, (status, data) => {
      if (status) {
        _this.setData({
          QRcodeImgSrc: data.data
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  QRCode(){
    let td = this.data, is_agent = this.data.userInfo.is_agent;
    let openid = wx.getStorageSync("token");   
    if (is_agent == 1) {
      this.setData({
        showQRcode:true
      });
    } else {
      wx.showToast({
        title: "您还未成为会员！",
        icon: "none",
        mask: true
      })
    }
  },
  myTeam(){
    let td = this.data, is_agent = this.data.userInfo.is_agent;
    if (is_agent && td.userInfo.son_number > 0){
        wx.navigateTo({
          url: "../my-team/my-team?list=" + JSON.stringify(td.userInfo)
        });
    } else if (is_agent && td.userInfo.son_number == 0) {
      wx.showToast({
        title: "暂无成员哦~",
        icon: "none",
        mask: true
      })
    }else{
      wx.showToast({
        title: "您还未成为会员！",
        icon:"none",
        mask:true
      })
    }
  },
  longpress(e){
    let td = this.data;
    wx.showModal({
      title: "",
      content: "是否保存图片相册",
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: "下载中...",
          });
          wx.downloadFile({
            url: td.QRcodeImgSrc,
            success: function (res) {
              wx.hideLoading();
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success: function (saveImg) {
                  wx.showToast({
                    title: "保存成功",
                    icon: "none",
                    mask: true
                  });
                },
                fail(saveImg) {
                  console.log(saveImg);
                }
              })
            }
          });
        }
      }
    })
  }
})